﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelliCalculator
{
    public class NoAvailableValueException : Exception { }
    public class ExitMessage : Exception { }
    public class ClearMessage : Exception { }
    public class ConsoleReader
    {
        public ConsoleReader(){}
        public ConsoleReader(string exit, string clear)
        {
            this.exit = exit.ToLower();
            this.clear = clear.ToLower();
        }

        protected string exit = @"exit";
        protected string clear = @"clear";

        /// <summary>
        /// 从控制台读取一串输入
        /// </summary>
        /// <param name="command">-可选- 返回第一个空格前的字符串，通常是指令。</param>
        /// <param name="b">true为将第一个字符串最为command；false为读取为double，command为null。</param>
        /// <returns>从控制台输入获取的double数组</returns>
        public double[] ReadDoubleArray(out string command, bool b)
        {
            if (b)
            {
                string[] text = Console.ReadLine().Split(' ');
                command = text[0];
                double[] dArray = new double[text.Length - 1];
                try
                {
                    for (int i = 0; i <= text.Length - 2; i++)
                    {
                        dArray[i] = Convert.ToDouble(text[i + 1]);
                    }
                }
                catch (FormatException e)
                {
                    Console.WriteLine(e);
                    throw new NoAvailableValueException();
                }
                catch (OverflowException e)
                {
                    Console.WriteLine(e);
                    throw new NoAvailableValueException();
                }
                return dArray;
            }
            else
            {
                string[] text = Console.ReadLine().Split(' ');
                if (text[0].ToLower().Equals("exit"))
                    throw new ExitMessage();

                switch (text[0].ToLower())
                {
                    case "exit":
                        throw new ExitMessage();
                    case "clear":
                        throw new ClearMessage();
                }
                command = null;
                double[] dArray = new double[text.Length];
                try
                {
                    for(int i = 0; i <= text.Length - 1; i++)
                    {
                        dArray[ i ] = Convert.ToDouble( text[i]);
                    }
                }
                catch (FormatException e)
                {
                    Console.WriteLine(e);
                    throw new NoAvailableValueException();
                }
                catch (OverflowException e)
                {
                    Console.WriteLine(e);
                    throw new NoAvailableValueException();
                }
                return dArray;
            }
        }

        /// <summary>
        /// 从命令行读取第一个空格前的字符串，通常为指令。
        /// </summary>
        /// <returns></returns>
        public string ReadCommand()
        {
            string Text = Console.ReadLine();
            return Text.Split(' ')[0];
        }
    }
}
